using FluentNHibernate.Mapping;
using NHibernate.Engine;
using RMH.Entities;

namespace RMH.DataAccess.Mappings
{
    public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Id(x => x.Id);
            Map(x => x.FirstName);
            Map(x => x.LastName);
            Map(x => x.MiddleName);
            Map(x => x.VehicleMake);
            Map(x => x.VehicleModel);
            Map(x => x.VehiclePlateNumber);
            Map(x => x.VehiclePlateState);
            Map(x => x.EmailAddress);            
            Map(x => x.PhoneNumber);
            Map(x => x.PhoneNumberAlternate);

            Component(c => c.Address, a =>
                                          {
                                              a.Map(x => x.City);
                                              a.Map(x => x.PostalCode);
                                              a.Map(x => x.State);
                                              a.Map(x => x.Street1);
                                              a.Map(x => x.Street2);
                                              a.Map(x => x.Street3);
                                          });

            DiscriminateSubClassesOnColumn<string>("PersonType")
                .SubClass<Patient>("P",
                                   patient =>
                                       {
                                           patient.Map(x => x.DateOfBirth);
                                           patient.Map(x => x.DateOfDeath);
                                           patient.References(x => x.County);
                                           
                                           patient.HasMany(x => x.Notes)                                               
                                               .AsBag()
                                               .Inverse()
                                               .Cascade.All();

                                           patient.HasMany(x => x.Stays)
                                                .AsBag()
                                                .Inverse()
                                                .Cascade.All();

                                           patient
                                               .HasManyToMany(x => x.Diagnoses)
                                               .Cascade.All();

                                           patient.HasMany(x => x.Relationships)
                                               .Cascade.All();
                                               
                                       })

                .SubClass<SupportPerson>("S",support => support.HasMany(x=>x.Relationships));

            DynamicInsert();
            DynamicUpdate();
   
        }
    }
}